package com.yunxi.service.tsp.dubbo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunxi.baseDataToHub.pojo.TSP.VehicleLocationVO;
import com.yunxi.baseDataToHub.service.tsp.TspDubboService;
import com.yunxi.core.base.Constant;
import com.yunxi.core.util.HttpUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import com.yunxi.service.app.dubbo.AppDubboServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2019/9/1.
 */
public class TspDubboServiceImpl implements TspDubboService {

    protected static final Logger LOGGER = LoggerFactory.getLogger(TspDubboServiceImpl.class);
    @Autowired
    private EaiLogsProducer eaiLogsProducer;
    @Value("${esb.switch}")
    private String esbSwitch;
    @Value("${TSP.URL}")
    private String TSP_URL;
    @Override
    public ApiResult findVehicleLocationPms2Tsp(VehicleLocationVO vehicleLocationVO) throws Exception {
        String vehicleLocation = JSONObject.toJSONString(vehicleLocationVO);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(vehicleLocation);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("PMS系统通过VIN获取车辆信息位置");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_TSP_VEHICLE_LOCATION);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(vehicleLocation, Constant.APP_LITTLE_ORDER_AUDIT, Constant.PMS, TSP_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail();
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (Integer.parseInt(String.valueOf(jsonResponse.get("code")))!=200){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail();
                }else {
                    apiResult = ResponseUtil.ok(String.valueOf(jsonResponse.get("data")));
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail();
        }
        return apiResult;
    }
}
